//
//  MainViewController.m
//  网络FM
//
//  Created by dengzhicheng on 15/5/21.
//  Copyright (c) 2015年 dengzhicheng.com. All rights reserved.
//

#import "MainViewController.h"
#import "AppDelegate.h"
#import "RecommendViewController.h"
#import "ClassifyViewController.h"
#import "SearchViewController.h"
#import "MineViewController.h"
#import "VOSegmentedControl.h"
@interface MainViewController ()<UIScrollViewDelegate>
@property (nonatomic, retain)UIScrollView *scroll;
@property (nonatomic, retain)VOSegmentedControl *segment;
@end

@implementation MainViewController
- (void)dealloc
{
    [_sideViewController release];
    [_segment release];
    [_scroll release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)loadView
{
    [super loadView];
    [self scrollView];
    [self segmentControl];
}
- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.navigationController.navigationBar.translucent = NO;
    //segmentControl放进导航标题
    self.navigationItem.titleView = _segment;
    
    
    //我的button
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width / 11, 26)];
    [button setImage:[UIImage imageNamed:@"我的1.png"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"我的2.png"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(mineAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [button release];
    
    // Do any additional setup after loading the view.
}



#pragma mark -
#pragma mark 创建scrollView
- (void)scrollView
{
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.scroll.contentSize = CGSizeMake(self.view.frame.size.width * 3, self.view.frame.size.height);
    self.scroll.delegate = self;
    _scroll.pagingEnabled = YES;
    _scroll.bounces = NO;

    
    //关联分类页面
    ClassifyViewController *classify = [[ClassifyViewController alloc] initWithNibName:nil bundle:nil];
    classify.view.frame = CGRectMake(0, 0, self.scroll.frame.size.width, self.scroll.frame.size.height);
    //添加子视图控制器
    [self addChildViewController:classify];
    [_scroll addSubview:classify.view];
    [classify release];
    
    //关联推荐页面
    RecommendViewController *recommend = [[RecommendViewController alloc] init];
    recommend.view.frame = CGRectMake(self.scroll.frame.size.width, 0, self.scroll.frame.size.width, self.scroll.frame.size.height);
    //添加子视图控制器
    [self addChildViewController:recommend];
    [_scroll addSubview:recommend.view];
    [recommend release];
    
    
    //关联搜索页面
    SearchViewController *search = [[SearchViewController alloc] initWithNibName:nil bundle:nil];
    search.view.frame = CGRectMake(self.scroll.frame.size.width * 2, 0, self.scroll.frame.size.width, self.scroll.frame.size.height);
    //添加子视图控制器
    [self addChildViewController:search];
    [_scroll addSubview:search.view];
    [search release];
    
    [self.view addSubview:_scroll];
    [_scroll release];
}
#pragma mark - 
#pragma mark 创建segmentControl
- (void)segmentControl
{
    self.segment = [[VOSegmentedControl alloc] initWithSegments:@[@{VOSegmentText:@"分类"},@{VOSegmentText:@"推荐"},@{VOSegmentText:@"搜索"}]];
    _segment.frame = CGRectMake(0, 0, self.view.frame.size.width / 1.3, 44);
    _segment.textColor = [UIColor blackColor];
    _segment.selectedTextColor = [UIColor colorWithRed:30 / 255.0 green:144 / 255.0 blue:255 / 255.0 alpha:1.0f];
    _segment.selectedBackgroundColor = [UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:1.0f];
    [_segment setBackgroundColor:[UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:1.0f]];

    _segment.indicatorStyle = VOSegCtrlIndicatorStyleTopLine;
    _segment.indicatorThickness = 0;
    _segment.selectedSegmentIndex = 0;
    [_segment addTarget:self action:@selector(segmentClicked:) forControlEvents:(UIControlEventValueChanged)];
    [self.segment release];
}
#pragma mark -
#pragma mark scrollView的偏移结束时与segmentedControl关联
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x == 0) {
        self.segment.selectedSegmentIndex = 0;
    } else if (scrollView.contentOffset.x == self.view.frame.size.width) {
        self.segment.selectedSegmentIndex = 1;
    } else if (scrollView.contentOffset.x == self.view.frame.size.width * 2) {
        self.segment.selectedSegmentIndex = 2;
    }
}



#pragma mark -
#pragma mark 点击segmentedControl,scrollCiew偏移
- (void)segmentClicked:(UISegmentedControl *)seg
{
    switch (seg.selectedSegmentIndex) {
        case 0:
        {
            [self.scroll setContentOffset:CGPointMake(0, 0)];
        }
            break;
        case 1:
        {
            [self.scroll setContentOffset:CGPointMake(self.view.frame.size.width, 0)];
        }
            break;
        case 2:
        {
            [self.scroll setContentOffset:CGPointMake(self.view.frame.size.width * 2, 0)];
        }
            break;
        default:
            break;
    }
}
#pragma mark -
#pragma mark 我的点击事件(拉出抽屉)
- (void)mineAction:(id)sender
{
    AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    self.sideViewController = [delegate sideViewController];
    [_sideViewController showLeftViewController:true];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
